<?php

    function recrusive(&$DB, &$translate, $lang, $query, $parent_ID, &$recordset, $columns) {

        $rows = $translate->res_array($DB->query($query, array($parent_ID))->result_array(), $lang);

        foreach ($rows as $row):

            $cur_ID = $row['ID'];

            $recordset[$cur_ID] = new stdClass();
            $recordset[$cur_ID]->ID = $cur_ID;

            foreach ($columns as $key => $value)
                $recordset[$cur_ID]->{$key} = $row[$value];

            if (count($DB->query($query, array($cur_ID))->result_array()) > 0)
                recrusive($DB, $translate, $lang, $query, $cur_ID, $recordset, $columns);

        endforeach;
    }

    function parseArrayTree($tree, $columns, $root = null) {
        $return = array();

        # Traverse the tree and search for direct children of the root
        foreach ($tree as $child => $parent) {

            # A direct child is found
            if ($parent->{$columns['parent_ID']} == $root) {

                # Remove item from tree (we don't need to traverse this again)
                unset($tree[$child]);

                # Append the child into result array and parse its children
                foreach ($columns as $key => $value)
                    $myArr[$key] = $parent->{$value};

                $myArr['children'] = parseArrayTree($tree, $columns, $child);
                $return[] = $myArr;
            }
        }
        return empty($return) ? null : $return;
    }

    function createRecordset(&$DB, &$translate, $lang, $query, $parent_ID, &$recordset, $columns) {

        $rows = $translate->res_array($DB->query($query)->result_array(), $lang);

        foreach ($rows as $row):

            $cur_ID = $row['ID'];

            $recordset[$cur_ID] = new stdClass();
            $recordset[$cur_ID]->ID = $cur_ID;

            foreach ($columns as $key => $value)
                $recordset[$cur_ID]->{$key} = $row[$value];

        endforeach;
    }

    function parseRecordset($tree, $columns) {
        $return = array();

        foreach ($tree as $child => $parent) {
            foreach ($columns as $key => $value)
                $myArr[$key] = $parent->{$value};

            $return[] = $myArr;
        }

        return empty($return) ? null : $return;
    }

    /*
     * @param $node['ID']
     * @param $node['title']
     * @param $_GET['level'] 
     */

    function printSelect(&$arrayTree, &$select, $parent_ID) {

        if (!is_null($arrayTree) && count($arrayTree) > 0)
            foreach ($arrayTree as $node):

                $selected = "";
                if ($node['ID'] == $parent_ID)
                    $selected = 'selected="selected"';

                $select .= '<option value="' . $node['ID'] . '"' . $selected . '>' . repeat_sign('&nbsp;', $_GET['level'] * 3) . '- ' . $node['title'] . '</option>';

                if (count($node['children']) > 0):
                    $_GET['level'] ++;
                    printSelect($node['children'], $select, $parent_ID);

                endif;

            endforeach;

        $_GET['level'] --;
    }

    /*
     * @param $node['ID']
     * @param $node['title']
     * @param $_GET['level'] 
     * @param $columns 
     */

    function printPost(&$arrayTree, &$select, $parent_ID, $columns) {

        if (!is_null($arrayTree) && count($arrayTree) > 0)
            foreach ($arrayTree as $node):

                foreach ($columns as $key => $value):

                    if ($key == 'title'):
                        if ($_GET['level'] == 0)
                            $node[$value] = "<b>".repeat_sign('&nbsp;', $_GET['level'] * 5) . '' . $node[$value].'</b>';
                        else
                            $node[$value] = repeat_sign('&nbsp;', $_GET['level'] * 5) . '- ' . $node[$value];

                    endif;

                    $myArr[$key] = $node[$value];

                endforeach;

                $_GET['items'][] = $myArr;

                if (isset($node['children']) && count($node['children']) > 0):
                    $_GET['level'] ++;
                    printPost($node['children'], $select, $parent_ID, $columns);

                endif;



            endforeach;

        $_GET['level'] --;
    }

    function parentCreate($columns, $query, $DB, $TRANSLATE, $LANG, $DATA_POST, $retType = 'SELECT') {

        $_GET['level'] = 0;
        $recordset = array();
        $root_parent_ID = 0;


        if (!isset($_GET['query'])):
            recrusive($DB, $TRANSLATE, $LANG, $query, $root_parent_ID, $recordset, $columns);
            $arrayTree = parseArrayTree($recordset, $columns, $root_parent_ID);
        else:
            createRecordset($DB, $TRANSLATE, $LANG, $query, $root_parent_ID, $recordset, $columns);
            $arrayTree = parseRecordset($recordset, $columns);
        endif;

        $html = "";

        $parent_ID = isset($DATA_POST) ? $DATA_POST['parent_ID'] : -1;

        switch ($retType) {
            case 'SELECT':
                printSelect($arrayTree, $html, $parent_ID);
                break;
            case 'POST':
                printPost($arrayTree, $html, $parent_ID, $columns);
                $html = $_GET['items'];
                break;
        }

        return $html;
    }
    